package com.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.model.RoleUser;
import com.app.model.User;
import com.app.req.UserRequest;
import com.app.resp.UserResponse;
import com.app.service.RoleService;
import com.app.service.RoleUserService;
import com.app.service.UserService;


@Controller
@RequestMapping("/web/user")
public class UserController {
	@Resource
	private UserService userService;
	@Resource
	private RoleUserService roleUserService;
	@Resource
	private RoleService roleService;
	@Resource
	private UserQuery userQuery;
	
	@RequestMapping(value = "")
	public String index() {
		return "auth/user";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Object getList(HttpServletRequest request) {
		/*Map data = new HashMap();
		List<User> list = userService.findAll();
		data.put("data", list);
		return data;*/
		return userQuery.queryPage(userQuery.getParams(request));
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Object save(UserRequest req) {
		String msg = null;
		try {
			User user = new User();
			user.setName(req.getName());
			user.setPwd(req.getName());
			userService.save(user);
			
			RoleUser roleUser = new RoleUser();
			roleUser.setUserId(userService.getByName(req.getName()).getId());
			roleUser.setRoleId(req.getRole());
			roleUser.setRoleName(roleService.getById(req.getRole()).getName());
			roleUserService.save(roleUser);
		} catch (Exception e) {
			msg = "新增用户出错";
		}
		return msg;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(UserRequest req) {
		String msg = null;
		try {
			User user = new User();
			user.setId(req.getId());
			user.setName(req.getName());
			user.setPwd(req.getPwd());
			userService.update(user);
			if(!req.getRole().equals(userService.getRole(req.getId()).getRoleId())){
				roleUserService.delete(req.getId(), userService.getRole(req.getId()).getRoleId());
				RoleUser roleUser = new RoleUser();
				roleUser.setUserId(req.getId());
				roleUser.setRoleId(req.getRole());
				roleUser.setRoleName(roleService.getById(req.getRole()).getName());
				roleUserService.save(roleUser);
			}
			
		} catch (Exception e) {
			msg = "更新用户出错";
		}
		return msg;
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Object getById(Long id) {
		User user = userService.getById(id);
		UserResponse resp = new UserResponse();
		BeanUtils.copyProperties(user, resp);
		resp.setRole(userService.getRole(id).getRoleId());
		return resp;
	}
	
	@RequestMapping("/getRoleList")
	@ResponseBody
	public Object getRoleList() {
		return userService.getRoleList();
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		String msg = null;
		try {
			userService.delete(id);
		} catch (Exception e) {
			msg = "删除用户出错";
		}
		return msg;
	}
}
